package org.sunsys.tic.search.engine.helper;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrServerException;
import org.sunsys.pdfbox.page.reader.PdfPagesReader;
import org.sunsys.solr.solrj.SolrDAO;

public class PdfPagesPublisher implements DocumentPublisher {

	//TODO Publishing Documents can be done using Hadoop or SCALA. Should implement SCALA code to publish documents
	
	private static Logger logger = Logger.getLogger(PdfPagesPublisherTest.class.getName());
	
	private static final String BOOK_ID = "bookId";
	private static final String PAGE_NO = "pageNo";
	private static final String PAGE_TEXT = "pageText";
	
	private SolrDAO solrDao = null;
	
	public PdfPagesPublisher() {
	}
	
	public void setSolrDao(SolrDAO solrDao) {
		this.solrDao = solrDao;
	}
	
	public SolrDAO getSolrDao() {
		return solrDao;
	}
	
	@Override
	public void publish(String softDocumentId, String documentPath) throws IOException {
		/*
		 * 1.	Identify the document TYPE
		 * 2.	Get the document reader factory for the document type
		 * 3.	Get the document reader using the document factory
		 * 4.	read each page text and publish the text to solr page by page
		 */
		
		PdfPagesReader reader = null;
		
		boolean documentUploaded = false;
		
		reader = new PdfPagesReader(documentPath);
		
		try{
			int numberOfPages = reader.getNumberOfPages();
			String pageText = null;
			
			Map<String, String> solrDocument = null;
			String []compositePrimaryKeyFiledsNames = {BOOK_ID, PAGE_NO};
				for(int i=0; i<numberOfPages; i++) {
					/* get Page text from document */
					pageText = reader.getPageText(i);
					
					if(pageText != null && pageText.trim().length() > 0) {
						/* use solrj to push the softDocumentId, and pageNo, and pageText */
						
						solrDocument = new HashMap<String, String>();
						
						solrDocument.put(BOOK_ID, softDocumentId);
						solrDocument.put(PAGE_NO, ""+i);
						solrDocument.put(PAGE_TEXT, pageText);
				
						solrDao.uploadData(solrDocument);
//						solrDao.deleteDocument(solrDocument, compositePrimaryKeyFiledsNames);
						
						solrDocument = null;
						
						documentUploaded = true;
						
						if( logger.isDebugEnabled()) {
							logger.debug("Published: SoftDocument Id:"+softDocumentId+"\tPageNo:"+i);
						}
					}else {
						if( logger.isDebugEnabled()) {
							logger.info("Ignored: SoftDocument Id:"+softDocumentId+"\tPageNo:"+i);
						}
					}
					
					
				}
		}catch(SolrServerException e1) {
			logger.error("SolrServerException while updating solr-document(s)", e1);
			throw new DocumentPublisherException("SolrServerException while updating solr-document(s)", e1);
		} catch(IOException e1) {
			logger.error("IOException while updating solr-document(s) ", e1);
			throw new DocumentPublisherException("IOException while updating solr-document(s)", e1);
		}catch(Exception e1){
			logger.error("Exception while updating solr-document(s) ", e1);
			throw new DocumentPublisherException("Exception while updating solr-document(s)", e1);
		} finally{
			if(documentUploaded) {
				/* commit solr changes */
				try {
					solrDao.commitChanges();
				}catch(SolrServerException e1) {
					logger.warn("SolrServerException while commiting the updated documents to solr", e1);
					throw new DocumentPublisherException("Exception while commiting the updated solr-documents", e1);
				}catch(IOException e2){
					logger.warn("IOException while commiting the updated documents to solr", e2);
				}
			}

			/* close the document */
			if( reader != null) {
				reader.closeDocument();
			}
		}
	}
}
